<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */
class wms_ctl_admin_deliverynote extends desktop_controller{

    var $workground = 'wms_ctl_admin_deliverynote';
    var $pagelimit = 10;
    var $stocknotice_model ;
    public function __construct($app)
    {
        parent::__construct($app);
        $this->member_model = $this->app->model('deliverynote');
        header("cache-control: no-store, no-cache, must-revalidate");
    }
    //备货通知单首页
    function index()
    {
        $filter='';
         //add by liyb 根据操作员带出所属组织进行过滤
         $org = kernel::single('desktop_user')->get_srcorg();
         $is_super = kernel::single('desktop_user')->is_super();
            if (!$is_super){
                    $filter['srcorg'] = $org;
                }
                //var_dump($filter);
        $params = array(
            'title' => 'OMS送货单',
            'actions' => array(
                    'new' => array(
                    'label' => '新建',
                    'href' => 'index.php?app=wms&ctl=admin_deliverynote&act=add_deliverynote',
                        'target' => '_blank',
                ),
                'confirm' => array(
                    'label' => '确认',
                    'submit' => 'index.php?app=wms&ctl=admin_deliverynote&act=doConfirm',
                ),
                'unconfirm' => array(
                    'label' => '取消确认',
                    'submit' => 'index.php?app=wms&ctl=admin_deliverynote&act=dounConfirm',
                ),
                // 'print' => array(
                //     'label' => '打印',
                //     'submit' => 'index.php?app=wms&ctl=admin_deliverynote&act=doPrint',
                //         'target' => '_blank',
                // ),
                'delete' => array(
                    'label' => '删除',
                    'submit' => 'index.php?app=wms&ctl=admin_deliverynote&act=doDelete',
                ),
            ),
            'base_filter' => $filter,
            //'base_filter' => '',
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag' => false,
            'use_buildin_recycle' => false,
            'use_buildin_export' => true ,
            'use_buildin_import' => false,
            'use_buildin_filter' => true,
            'allow_detail_popup' => true,
            'max_actions' => 8,
            'use_view_tab' => false,
            'orderBy' => 'create_time desc',
        );
        $this->finder('wms_mdl_deliverynote', $params);

    }
    function doaSave(){


        $this->begin();
        $delivery = $_POST['delivery'];
		$deliverynote_bn = $_POST['deliverynote_bn'];
        $value = $_POST['delivery_name'];
		$deliverynoteId = $this->app->model('deliverynote');
        $deliverynote = $this->app->model('deliverynote_line');
        $deliveryMdl = app::get('wms')->model('deliverynote_line');
        $delivery_id=$deliverynoteId->dump(array('deliverynote_bn'=>$deliverynote_bn),'id');
		/*$delivery_bn=app::get('wms')->model('deliverynote_line')->getList('delivery_bn',array('deliverynote_id'=>$delivery_id['id']));
		foreach($delivery_bn as $k => $v){
			$aa=$deliverynote->db->exec("update sdb_wms_delivery set carno ='.$value.' where  delivery_bn =".$v['delivery_bn']);
		}*/
        $deliverynote->db->exec("UPDATE sdb_wms_delivery AS wd LEFT JOIN sdb_wms_deliverynote_line wdl ON wdl.delivery_id = wd .delivery_id LEFT JOIN sdb_wms_deliverynote wdn ON wdn.id = wdl.deliverynote_id SET wd.carno = '$value' where  wdn.deliverynote_bn='".$deliverynote_bn."' ");

        $deliverynote->db->exec("update sdb_wms_deliverynote_line set carno ='$value' where  deliverynote_id =".$delivery_id['id']);

        $this->end(true, '已保存');


    }
    //打印动作
    function doPrint()
    {
        $this->begin("index.php?app=wms&ctl=admin_deliverynote&act=index");
        $this->_request    = kernel::single('base_component_request');
        $order_info        = $this->_request->get_post();

        $deliverynote_model = $this->app->model('deliverynote');
        $res=array();
        $comp_data = $deliverynote_model->getDelivery($order_info['id'][0]);
        foreach ($comp_data as $key => $value)
        {
            $compline_data = $deliverynote_model->getDeliveryInfo($order_info['id'][0],$value['AXCustomer']);
            $res[$value['AXCustomer']]=array(
                "deliverynote_bn"=> $value['deliverynote_bn'],
                "AXCustomer"=>  $value['AXCustomer'],
                "contact"=> $value['contact'],
                "contacttel"=>  $value['contacttel'],
                "shipaddress"=> $value['shipaddress'],
                "operator"=>$value['operator'],
                "soprojecter"=> $value['soprojecter'],
                "accountmethod"=> $value['accountmethod'],
                "shortname"=> $value['shortname'],
                "customername"=> $value['customername'],
                "carno"=> $value['carno'],
                "axshipno"=> $value['axshipno'],
                "axorderno"=> $value['axorderno'],

                "compline_data"=>$compline_data
            );
        }
        $this->pagedata['res'] = $res;
        $this->singlepage('admin/deliverynote/deliverynote_print.html');
    }
    //添加备货
    function add_deliverynote()
    {
        $operator = kernel::single('desktop_user')->get_name();
        $this->pagedata['operator'] = $operator;
        //add by liyb 获取操作员所属组织
        $org = kernel::single('desktop_user')->get_srcorg();
        $this->pagedata['srcorg'] = $org;
        
        //add by ivan 获取操作员所属区域
        $area = kernel::single('desktop_user')->get_area();
        $this->pagedata['area'] = $area;
        //var_dump($area);
        /*
        //add by IVAN20190216 根据判断过滤权限
        $Obranch = app::get('ome')->model('branch');
        $branch_ids = $Obranch->getList('branch_id,name,uname,phone,mobile');
        foreach ($branch_ids as $key => $value) {
            $branch_data[$value['branch_id']]=$value['name'];
        }
        $clearingtable['branch_id'] = $_POST['branch_id'];
        $this->pagedata['branch_data'] = $branch_data;*/


        $y = date("Y");
        $m = date("m");
        $d = date("d");
        $time_begin= mktime(0,0,0,$m,$d,$y);
        $time_end= mktime(23,59,59,$m,$d,$y);
        $delivery_model = $this->app->model('deliverynote');
        $filter['create_time|between'] = array($time_begin, $time_end);
        $delivery= $delivery_model->getList('deliverynote_bn',$filter,0,1,'create_time desc');

        if($delivery[0]['deliverynote_bn']=='')
        {
            $this->pagedata['cur_date'] ='ED-'.date('ymd',time()).'01';
        }
        else
        {
            $bn=substr($delivery[0]['deliverynote_bn'] , 9 , 2);
            $num=intval($bn)+1;
            if($num<=9)
            {
                $this->pagedata['cur_date'] ='ED-'.date('ymd',time()).'0'.$num;
            }else
            {
                $this->pagedata['cur_date'] ='ED-'.date('ymd',time()).$num;
            }
        }

        $this->pagedata['time'] =time();
        $this->pagedata['note_date'] =date('Y-m-d',time());
        $this->singlepage('admin/deliverynote/deliverynote_add.html');
    }

    /**
     * 基础物料列表弹窗数据获取方法
     *
     * @param Void
     * @return String
     */
    function finddelivery($supplier_id=null)
    {
        $delivery_start = strtotime($_GET['note_date']);
        $delivery_end = strtotime($_GET['note_date'])+86399;

            /*
             * 获取操作员管辖仓库
             */
            $oBranch = app::get('ome')->model('branch');
            $is_super = kernel::single('desktop_user')->is_super();
            if (!$is_super){
                $branch_ids = $oBranch->getBranchByUser(true);
                if ($branch_ids){
                    $base_filter['branch_id'] = $branch_ids;
                }else{
                    $base_filter['branch_id'] = 'false';
                }
            }

        $base_filter['status'] = '3';
        $base_filter['process_status'] = '7';
        $base_filter['is_ship'] = 'true';
        $base_filter['is_form'] = 'true';
        $base_filter['is_note'] = 'false';
        $base_filter['delivery_start'] = $delivery_start;
        $base_filter['delivery_end'] = $delivery_end;
        $base_filter['deliverytype'] = array('pickup','delivery','auto','batch','single');

        if(!$_POST['shop_id']){
            $base_filter['shop_id'] = $this->shopIds;
        }else{
            $base_filter['shop_id'] = $_POST['shop_id'];
        }

        $params = array(
                'title'=>'发货单',
                'use_buildin_new_dialog' => false,
                'use_buildin_set_tag'=>false,
                'use_buildin_recycle'=>false,
                'use_buildin_export'=>false,
                'use_buildin_import'=>false,
                'use_buildin_filter'=>true,
                'base_filter' => $base_filter,
        );
        $this->finder('wms_mdl_delivery', $params);
    }

    function getdeliverys()
    {
        $pro_id = $_POST['product_id'];
        $shopId = $this->shopIds;

        if (is_array($pro_id)){
            $filter['delivery_id'] = $pro_id;
        }


            /*
             * 获取操作员管辖仓库
             */
            $oBranch = app::get('ome')->model('branch');
            $is_super = kernel::single('desktop_user')->is_super();
            if (!$is_super){
                $branch_ids = $oBranch->getBranchByUser(true);
                if ($branch_ids){
                    $filter['branch_id'] = $branch_ids;
                }else{
                    $filter['branch_id'] = 'false';
                }
            }

        
		if (isset($_POST['filter']['advance']) && $_POST['filter']['advance'])
		{
			$arr_filters    = explode(',', $_POST['filter']['advance']);
			foreach ($arr_filters as $obj_filter)
			{
				$arr    = explode('=', $obj_filter);
				$filter[$arr[0]]    = $arr[1];
			}
			if($filter['_delivery_time_search'] == 'than' || $filter['_delivery_time_search'] == 'lthan'){
				$filter['_DTYPE_TIME'][0] = $filter['_DTYPE_TIME[]'];
				$filter['_DTIME_']['H']['delivery_time'] = $filter['_DTIME_[H][delivery_time]'];
				$filter['_DTIME_']['M']['delivery_time'] = $filter['_DTIME_[M][delivery_time]'];
			}
			if($filter['_delivery_time_search'] == 'between'){
				$filter['_DTYPE_TIME'][0] = $filter['_DTYPE_TIME[]'];
				$filter['_DTIME_']['H']['delivery_time_from'] = $filter['_DTIME_[H][delivery_time_from]'];
				$filter['_DTIME_']['M']['delivery_time_from'] = $filter['_DTIME_[M][delivery_time_from]'];
				$filter['_DTIME_']['H']['delivery_time_to'] = $filter['_DTIME_[H][delivery_time_to]'];
				$filter['_DTIME_']['M']['delivery_time_to'] = $filter['_DTIME_[M][delivery_time_to]'];
			}
		}
        $filter['process_status'] = '7';
        $filter['status'] = '3';
        $filter['is_ship'] = 'true';
        $filter['is_form'] = 'true';
        $filter['is_note'] = 'false';
        $filter['deliverytype'] = array('pickup','delivery','auto','batch','single');

        $delivery_model = $this->app->model('delivery');
        $delivery= $delivery_model->getList('delivery_bn,delivery_id,shop_id',$filter);

        if (!empty($delivery))
        {
            $row =array();
            if(!$shopId){
                foreach ($delivery as $k => $item)
                {
                        $row['product_id']=$item['delivery_id'];
                        $row['delivery_bn']=$item['delivery_bn'];
                        $rows[]= $row;
                }
            }else{
                foreach ($delivery as $k => $item)
                {
                    if(in_array($item['shop_id'],$shopId)){
                        $row['product_id']=$item['delivery_id'];
                        $row['delivery_bn']=$item['delivery_bn'];
                        $rows[]= $row;
                    }
                }
            }
        }

        //error_log(date('Y-m-d H:i:s').'订单/'.var_export(array($rows),true),3,DATA_DIR.'/test23.log');

        echo "window.autocompleter_json=".json_encode($rows);
    }

    //保存备货通知单
    function doSave()
    {
        $this->begin();
        if(!$_POST['delivery_ID']){
            $this->end(false, '发货单为空');
        }
        $deliveryMdl = app::get('wms')->model('delivery');
        $shopMdl = app::get('ome')->model('shop');
        $deliverynote_model = $this->app->model('deliverynote');
        $deliverynote_line_model = $this->app->model('deliverynote_line');
        $delivery_ID = $_POST['delivery_ID'];
        $name = $_POST['purchase_name'];
        $memo = $_POST['memo'];
        $operator = $_POST['operator'];
        $note_date = $_POST['note_date'];
        //add by liyb
        $srcorg = $_POST['srcorg'];
        //add by ivan
        $area = $_POST['area'];
        /*//add by ivan
        $branch = $_POST['branch'];*/
        $arr_order='';

        foreach ($delivery_ID as $k => $v) {

            $shopId = $deliveryMdl->getList('shop_id', array('delivery_id'=>$v));
            foreach ($shopId as $r => $row) {
                $sendType = $shopMdl->getList('sendType', array('shop_id'=>$row['shop_id']));
                if($sendType[0]['sendType'] == 1){
                    $sendTypeNum1 = 1;
                }else{
                    $sendTypeNum2 = 2;
                }
            }
        }
        if($sendTypeNum1 && $sendTypeNum2){
            $this->end(false, '同时存在按天发货和整车发货店铺的送货单,请使用指定店铺筛选！');
        }

        $y = date("Y",$note_date);
        $m = date("m",$note_date);
        $d = date("d",$note_date);
        $time_begin= strtotime($note_date);
        $time_end= strtotime($note_date)+2;
        $filter['deliverynote_time|between'] = array($time_begin, $time_end);
        $delivery= $deliverynote_model->getList('deliverynote_bn',$filter,0,1,'create_time desc');
        if($delivery[0]['deliverynote_bn']=='')
        {
            $noteDate = date('ymd',strtotime($note_date));
            $cur_notedate = date('ymd',strtotime($note_date)).'01';
            $cur_date ='ED-'.date('ymd',strtotime($note_date)).'01';
        }
        else
        {
            $bn=substr($delivery[0]['deliverynote_bn'] , 9 , 2);
            $num=intval($bn)+1;
            if($num<=9)
            {
            	$noteDate = date('ymd',strtotime($note_date));
                $cur_notedate = date('ymd',strtotime($note_date)).'0'.$num;
                $cur_date ='ED-'.date('ymd',strtotime($note_date)).'0'.$num;
            }
            else
            {
            	$noteDate = date('ymd',strtotime($note_date));
                $cur_notedate =date('ymd',strtotime($note_date)).$num;
                $cur_date ='ED-'.date('ymd',strtotime($note_date)).$num;
            }
        }

        $arr_order['deliverynote']['deliverynote_bn']=$cur_date;
        $arr_order['deliverynote']['remark']=$_POST['memo'];
        $arr_order['deliverynote']['operator']=$_POST['operator'];
        $arr_order['deliverynote']['create_time']=time();
        $arr_order['deliverynote']['deliverynote_time']=strtotime($note_date)+1;
        $arr_order['deliverynote']['sendType']=$_POST['sendType'];
        //add by liyb
        $arr_order['deliverynote']['srcorg']=$_POST['srcorg'];
        //add by ivan
        $arr_order['deliverynote']['area']=$_POST['area'];
        /*//add by ivan
        $arr_order['deliverynote']['branch']=$_POST['branch'];*/

        $deliverylist = implode(',',$delivery_ID);
        $db = kernel::database();
        $sql = "SELECT id FROM sdb_wms_deliverynote_line WHERE delivery_id IN (".$deliverylist.")";
        //add by ivan
        /*$sql = "SELECT id,branch_id FROM sdb_wms_deliverynote_line dnl
                LEFT JOIN sdb_wms_delivery d on ( dnl.delivery_id = d.delivery_id) 
                WHERE delivery_id IN (".$deliverylist.")";*/
        $is_delivery = $db->select($sql);

        if($is_delivery){
            $this->end(false, '生成送货单存在多人同时操作，请关闭后重试');
        }
        //创建OMS备货通知单
        $res = $deliverynote_model->insert($arr_order['deliverynote']);
        //var_dump($res);

        $orders_obj = $this->app->model('delivery');
        $shopmodel = app::get('ome')->model('shop');
        $deliverynote_id = $deliverynote_model ->getList('id',array('deliverynote_bn'=>$cur_date));
        $de_num  =1;
        foreach ($delivery_ID as $key => $value)
        {
            $deliveryBillLib = kernel::single('wms_delivery_bill');
            $logino = $deliveryBillLib->getPrimaryLogiNoById($value);
            $orders = $orders_obj->getList('delivery_bn,delivery_id,shop_id,deliverytype,carno,logi_id,logi_name,bnsContent', 
                array('delivery_id'=>$value,'deliverytype'=> array('pickup','delivery','auto','batch','single')), 0, 1);
           
            $shops = $shopmodel->getList('*', array('shop_id'=>$orders[0]['shop_id']), 0, 1);
            $arr_line='';
            $arr_line['deliverynote']['delivery_id']=$value;
            $arr_line['deliverynote']['create_time']=time();
            $arr_line['deliverynote']['deliverynote_id']=$res;
            $arr_line['deliverynote']['delivery_bn']=$orders[0]['delivery_bn'];
            $arr_line['deliverynote']['deliverytype']=$orders[0]['deliverytype'];
            $arr_line['deliverynote']['logi_id']=$orders[0]['logi_id'];
            $arr_line['deliverynote']['logi_no']=$logino;
            $arr_line['deliverynote']['logi_name']=$orders[0]['logi_name'];
            $arr_line['deliverynote']['carno']=$orders[0]['carno'];
            $cnts = unserialize($orders[0]['bnsContent']);
            $arr_line['deliverynote']['bnsContent']=implode(',',$cnts);
            $arr_line['deliverynote']['shop_id']=$orders[0]['shop_id'];
            $arr_line['deliverynote']['shop_name']=$shops[0]['name'];
            $arr_line['deliverynote']['shortname']=$shops[0]['shortname'];
            $arr_line['deliverynote']['ax_name']=$shops[0]['AXCustomer'];
            $arr_line['deliverynote']['ax_shopname']=$shops[0]['customername'];
            //$arr_line['deliverynote']['branch_id']=$orders[0]['branch_id'];

            if($shops[0]['sendType'] ==2){
                $arr_line['deliverynote']['axshipno']='ED-'.$shops[0]['shortname'].$cur_notedate;
                $arr_line['deliverynote']['axorderno']='ES'.$shops[0]['shortname'].$noteDate.'01';
            }else{
                    $arr_line['deliverynote']['axshipno']='ED-'.$shops[0]['shortname'].$cur_notedate;
                    $arr_line['deliverynote']['axorderno']='ES'.$shops[0]['shortname'].$cur_notedate;

            }
            $deliverynote_line_model->insert($arr_line['deliverynote']);

            $orders_obj->db->exec("update sdb_wms_delivery set is_note = 'true' where  delivery_id in(".$value.")");
        }

        $this->end(true, '已完成');
    }

    //确认动作
    function doDelete()
    {
        //$max_input_vars = ini_get('max_input_vars');
        ini_set('memory_limit','512M');
        set_time_limit(600);
        $this->begin("index.php?app=wms&ctl=admin_deliverynote&act=index");
        $this->_request    = kernel::single('base_component_request');
        $order_info        = $this->_request->get_post();

        $deliverynote_line_model = $this->app->model('deliverynote_line');
        $deliverynote_model = $this->app->model('deliverynote');
        $orders_obj = $this->app->model('delivery');

        if ($order_info['isSelectedAll']=='_ALL_') {

            $orders = $deliverynote_model->getList('*', array(), 0, -1);
            foreach ($orders as $key => $order) {
                $value = $order['id'];
                if ($order['status'] == 'unconfirmed') {
                    $deliverynote_line = $deliverynote_line_model->getList('*', array('deliverynote_id' => $value));
                    foreach ($deliverynote_line as $key => $delivery_id) {
                        $orders_obj->db->exec("update sdb_wms_delivery set is_note = 'false' where  delivery_id in(" . $delivery_id['delivery_id'] . ")");
                    }

                    $deliverynote_line_model->db->exec("delete from sdb_wms_deliverynote_line  where  deliverynote_id in(" . $value . ");");
                    $deliverynote_line_model->db->exec("delete from sdb_wms_deliverynote  where  id in(" . $value . ");");
                } else {
                    $this->end(false, '送货单号【' . $orders[0]['deliverynote_bn'] . '】状态已确认，删除失败！');
                }
            }

        }else {
            $input = file_get_contents("php://input");
            $input_array = explode('&', $input);
            $number = count($input_array);
            /*if ($number > $max_input_vars) {
                $msg =  "多选不能超过系统表单最大提交数【".$max_input_vars."】条";
                $this->end(false, $msg);
            }*/
            foreach ($order_info['id'] as $key_o => $value) {
                $orders = $deliverynote_model->getList('*', array('id' => $value), 0, 1);

                if ($orders[0]['status'] == 'unconfirmed') {
                    $deliverynote_line = $deliverynote_line_model->getList('*', array('deliverynote_id' => $value));
                    foreach ($deliverynote_line as $key => $delivery_id) {
                        $orders_obj->db->exec("update sdb_wms_delivery set is_note = 'false' where  delivery_id in(" . $delivery_id['delivery_id'] . ")");
                    }

                    $deliverynote_line_model->db->exec("delete from sdb_wms_deliverynote_line  where  deliverynote_id in(" . $value . ");");
                    $deliverynote_line_model->db->exec("delete from sdb_wms_deliverynote  where  id in(" . $value . ");");
                } else {
                    $this->end(false, '送货单号【' . $orders[0]['deliverynote_bn'] . '】状态已确认，删除失败！');
                }
            }
        }
        $this->end(true, '已完成');
    }

    //确认动作
    function doConfirm()
    {
        //$max_input_vars = ini_get('max_input_vars');
        ini_set('memory_limit','512M');
        set_time_limit(600);
        $this->begin("index.php?app=wms&ctl=admin_deliverynote&act=index");
        $this->_request    = kernel::single('base_component_request');
        $order_info        = $this->_request->get_post();

        $deliverynote_model = $this->app->model('deliverynote');
        $deliverynote_line_model = $this->app->model('deliverynote_line');
        $orders_obj = $this->app->model('delivery');
        if ($order_info['isSelectedAll']=='_ALL_') {
            $orders = $deliverynote_model->getList('*', array(), 0, -1);
            foreach ($orders as $key => $order)
            {
                $value = $order['id'];
                if($order['status']=='unconfirmed')
                {
                    $deliverynote_model->db->exec("update sdb_wms_deliverynote set status = 'confirmed',last_modify=".time()." where  id in(".$value.")");
                }
                else
                {
                    $this->end(false, '发货单号【'.$order['deliverynote_bn'].'】状态已确认，确认失败！');
                }
            }
        }else{
            $input = file_get_contents("php://input");
            $input_array = explode('&', $input);
            $number = count($input_array);
            /*if ($number > $max_input_vars) {
                $msg =  "多选不能超过系统表单最大提交数【".$max_input_vars."】条";
                $this->end(false, $msg);
            }*/
            foreach ($order_info['id'] as $key_o => $value)
            {
                $orders = $deliverynote_model->getList('*', array('id'=>$value), 0, 1);
                if($orders[0]['status']=='unconfirmed')
                {
                    $deliverynote_model->db->exec("update sdb_wms_deliverynote set status = 'confirmed',last_modify=".time()." where  id in(".$value.")");

                }else
                {
                    $this->end(false, '发货单号【'.$orders[0]['deliverynote_bn'].'】状态已确认，确认失败！');
                }
            }
        }
        $this->end(true, '已完成');
    }

    function dounConfirm()
    {
        //$max_input_vars = ini_get('max_input_vars');
        ini_set('memory_limit','512M');
        set_time_limit(600);
        $this->begin("index.php?app=wms&ctl=admin_deliverynote&act=index");
        $this->_request    = kernel::single('base_component_request');
        $order_info        = $this->_request->get_post();

        $deliverynote_model = $this->app->model('deliverynote');
        $deliverynote_line_model = $this->app->model('deliverynote_line');
        $orders_obj = $this->app->model('delivery');
        $deliverycomp_line_model = $this->app->model('deliverycomp_line');

        if ($order_info['isSelectedAll']=='_ALL_') {
            $orders = $deliverynote_model->getList('*', array(), 0, -1);
            foreach ($orders as $key => $order) {
                $value = $order['id'];
                $deliverycomp_line = $deliverycomp_line_model->getList('deliverynote_id', array('deliverynote_id'=>$value), 0, 1);
                if($deliverycomp_line[0]['deliverynote_id']>0)
                {
                    $this->end(false, '送单号【'.$order['deliverynote_bn'].'】已做发货完成，删除失败！');
                }
                if($order['status']=='confirmed')
                {
                    $deliverynote_model->db->exec("update sdb_wms_deliverynote set status = 'unconfirmed',last_modify=".time()." where  id in(".$value.")");
                }
                else
                {
                    $this->end(false, '发货单号【'.$order['deliverynote_bn'].'】状态未确认，取消确认失败！');
                }
            }
        }else{
            $input = file_get_contents("php://input");
            $input_array = explode('&', $input);
            $number = count($input_array);
            /*if ($number > $max_input_vars) {
                $msg =  "多选不能超过系统表单最大提交数【".$max_input_vars."】条";
                $this->end(false, $msg);
            }*/
            foreach ($order_info['id'] as $key_o => $value)
            {
                $orders = $deliverynote_model->getList('*', array('id'=>$value), 0, 1);
                $deliverycomp_line = $deliverycomp_line_model->getList('deliverynote_id', array('deliverynote_id'=>$value), 0, 1);
                if($deliverycomp_line[0]['deliverynote_id']>0)
                {
                    $this->end(false, '送单号【'.$orders[0]['deliverynote_bn'].'】已做发货完成，删除失败！');
                }
                if($orders[0]['status']=='confirmed')
                {
                    $deliverynote_model->db->exec("update sdb_wms_deliverynote set status = 'unconfirmed',last_modify=".time()." where  id in(".$value.")");
                }
                else
                {
                    $this->end(false, '发货单号【'.$orders[0]['deliverynote_bn'].'】状态未确认，取消确认失败！');
                }
            }
        }
        $this->end(true, '已完成');
    }
    function deliedit($id)
    {
        $this->begin('index.php?app=wms&ctl=admin_deliverynote&act=index');

        if(isset($_GET['id']))
        {
            $id = $_GET['id'];


            //$deliverynote_model = app::get('wms')->model('deliverynote');
            $deliverynote_line_model = app::get('wms')->model('deliverynote_line');
            $deliverynote_model = app::get('wms')->model('deliverynote');

            $data=$deliverynote_line_model->dump(array('deliverynote_id'=>$id),'*');
            $deliverynote=$deliverynote_model->dump(array('id'=>$id),'deliverynote_bn');
			$data['deliverynote_bn'] = $deliverynote['deliverynote_bn'];
            $this->pagedata['deliverynote'] = $data;
        }
        $this->singlepage("admin/deliverynote/deliverynote_edit.html");
    }



    function getDeliveryLine($id)
    {
        if(isset($_GET['id']))
        {
            $id = $_GET['id'];
            //[{"product_id":"3","num":"4123123","delete":1,"visibility":"true"},
            // {"product_id":"4","num":"1","delete":1,"visibility":"true"}]

            $piObj = app::get('wms')->model('deliverynote_line');
            $rows = array();
            $items = $piObj->getList('delivery_id,delivery_bn',array('deliverynote_id'=>$id));
            if ($items){
                foreach ($items as $k => $value)
                {
                    $rows[$k]=array(
                        "product_id"=> $value['delivery_id'],
                        /*"branch_id"=> $value['branch_id'],*/
                        "delivery_bn"=>  $value['delivery_bn']
                    );
                }
            }
            echo json_encode($rows);
        }
    }
}
